using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
   public class tbTraloiDAL
    {     
        private SqlConnection cn = null;
        public tbTraloiDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbTraloi tl)
        {
            SqlCommand cm = new SqlCommand("Traloi_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Mabl", tl.Mabl);
            cm.Parameters.AddWithValue("@Traloi", tl.Traloi);
            cm.ExecuteNonQuery();
        }
        public void Update(tbTraloi tl)
        {
            SqlCommand cm = new SqlCommand("Traloi_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Matrl", tl.Matrl);
            cm.Parameters.AddWithValue("@Mabl", tl.Mabl);
            cm.Parameters.AddWithValue("@Traloi", tl.Traloi);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("Traloi_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Matrl", iD);
            cm.ExecuteNonQuery();
        }
        public tbTraloiCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("Traloi_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbTraloiCollection tlCollection = new tbTraloiCollection();
            while (dr.Read()) 
            {
                tbTraloi tl = new tbTraloi();
                tl.Matrl = int.Parse(dr.GetValue(0).ToString());
                tl.Mabl = int.Parse(dr.GetValue(1).ToString());
                tl.Traloi = dr.GetValue(2).ToString();
                tlCollection.Add(tl);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return tlCollection;
        }
    }
}
